﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WAI
{
    public class Step_FolderRights : InternalWizardPage
    {
        private TreeView treeView1;
        private System.ComponentModel.IContainer components = null;
        static List<String> _AccessList = new List<string>();
        private ImageList iconList;
        const int expand_deep = 1;
        private CheckBox check_write;
        private CheckBox check_modify;
        private CheckBox check_full;
        private Label label2;
        private CheckBox check_readonly;
        private Label label3;
        private Label label4;
        private Label label1;

        public Step_FolderRights()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitializeComponent call
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Step_FolderRights));
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.iconList = new System.Windows.Forms.ImageList(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.check_write = new System.Windows.Forms.CheckBox();
            this.check_modify = new System.Windows.Forms.CheckBox();
            this.check_full = new System.Windows.Forms.CheckBox();
            this.label2 = new System.Windows.Forms.Label();
            this.check_readonly = new System.Windows.Forms.CheckBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // Banner
            // 
            this.Banner.Size = new System.Drawing.Size(759, 64);
            this.Banner.Subtitle = "Please select the directories where the IIS-User needs write access";
            this.Banner.Title = "Directory Access";
            // 
            // treeView1
            // 
            this.treeView1.ImageIndex = 0;
            this.treeView1.ImageList = this.iconList;
            this.treeView1.Location = new System.Drawing.Point(3, 73);
            this.treeView1.Name = "treeView1";
            this.treeView1.SelectedImageIndex = 0;
            this.treeView1.Size = new System.Drawing.Size(508, 354);
            this.treeView1.TabIndex = 1;
            this.treeView1.AfterCollapse += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCollapse);
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
            this.treeView1.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterExpand);
            // 
            // iconList
            // 
            this.iconList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("iconList.ImageStream")));
            this.iconList.TransparentColor = System.Drawing.Color.Transparent;
            this.iconList.Images.SetKeyName(0, "folder_open.ico");
            this.iconList.Images.SetKeyName(1, "Folder_Back.ico");
            this.iconList.Images.SetKeyName(2, "Generic_Document.ico");
            this.iconList.Images.SetKeyName(3, "AudioFile.ico");
            this.iconList.Images.SetKeyName(4, "Generic_Application.ico");
            this.iconList.Images.SetKeyName(5, "generic_picture.ico");
            this.iconList.Images.SetKeyName(6, "GenericMovieClip.ico");
            this.iconList.Images.SetKeyName(7, "zippedFile.ico");
            this.iconList.Images.SetKeyName(8, "XAML_file.ico");
            this.iconList.Images.SetKeyName(9, "Settings.ico");
            this.iconList.Images.SetKeyName(10, "batfile.ico");
            this.iconList.Images.SetKeyName(11, "cab.ico");
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(530, 159);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(57, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Filename";
            this.label1.Visible = false;
            // 
            // check_write
            // 
            this.check_write.AutoSize = true;
            this.check_write.Location = new System.Drawing.Point(533, 185);
            this.check_write.Name = "check_write";
            this.check_write.Size = new System.Drawing.Size(48, 17);
            this.check_write.TabIndex = 4;
            this.check_write.Text = "write";
            this.check_write.UseVisualStyleBackColor = true;
            this.check_write.CheckedChanged += new System.EventHandler(this.check_write_CheckedChanged);
            // 
            // check_modify
            // 
            this.check_modify.AutoSize = true;
            this.check_modify.Location = new System.Drawing.Point(533, 208);
            this.check_modify.Name = "check_modify";
            this.check_modify.Size = new System.Drawing.Size(56, 17);
            this.check_modify.TabIndex = 5;
            this.check_modify.Text = "modify";
            this.check_modify.UseVisualStyleBackColor = true;
            this.check_modify.CheckedChanged += new System.EventHandler(this.check_modify_CheckedChanged);
            // 
            // check_full
            // 
            this.check_full.AutoSize = true;
            this.check_full.Location = new System.Drawing.Point(533, 231);
            this.check_full.Name = "check_full";
            this.check_full.Size = new System.Drawing.Size(39, 17);
            this.check_full.TabIndex = 6;
            this.check_full.Text = "full";
            this.check_full.UseVisualStyleBackColor = true;
            this.check_full.CheckedChanged += new System.EventHandler(this.check_full_CheckedChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(530, 282);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(84, 13);
            this.label2.TabIndex = 7;
            this.label2.Text = "File attributes";
            // 
            // check_readonly
            // 
            this.check_readonly.AutoSize = true;
            this.check_readonly.Location = new System.Drawing.Point(533, 308);
            this.check_readonly.Name = "check_readonly";
            this.check_readonly.Size = new System.Drawing.Size(66, 17);
            this.check_readonly.TabIndex = 8;
            this.check_readonly.Text = "readonly";
            this.check_readonly.UseVisualStyleBackColor = true;
            this.check_readonly.CheckedChanged += new System.EventHandler(this.check_readonly_CheckedChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(529, 73);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(105, 20);
            this.label3.TabIndex = 10;
            this.label3.Text = "Permissions";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(530, 118);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(129, 17);
            this.label4.TabIndex = 11;
            this.label4.Text = "File/Folder name";
            // 
            // Step_FolderRights
            // 
            this.Controls.Add(this.check_readonly);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.check_full);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.treeView1);
            this.Controls.Add(this.check_modify);
            this.Controls.Add(this.check_write);
            this.Controls.Add(this.label1);
            this.Name = "Step_FolderRights";
            this.Size = new System.Drawing.Size(759, 491);
            this.Load += new System.EventHandler(this.Step_FolderRights_Load);
            this.Leave += new System.EventHandler(this.Step_FolderRights_Leave);
            this.SetActive += new System.ComponentModel.CancelEventHandler(this.Step_FolderRights_SetActive);
            this.Controls.SetChildIndex(this.label1, 0);
            this.Controls.SetChildIndex(this.check_write, 0);
            this.Controls.SetChildIndex(this.check_modify, 0);
            this.Controls.SetChildIndex(this.treeView1, 0);
            this.Controls.SetChildIndex(this.label2, 0);
            this.Controls.SetChildIndex(this.check_full, 0);
            this.Controls.SetChildIndex(this.label3, 0);
            this.Controls.SetChildIndex(this.label4, 0);
            this.Controls.SetChildIndex(this.check_readonly, 0);
            this.Controls.SetChildIndex(this.Banner, 0);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private void Step_FolderRights_SetActive(object sender, CancelEventArgs e)
        {
            SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
        }

        class myTreeNode : TreeNode
        {
            public myTreeNode(string fp, string type)
            {
                this.Text = fp.Substring(fp.LastIndexOf("\\") + 1);
                this.Name = fp;

                if (type == "dir")
                {
                    //set image to folderimage
                    this.ImageIndex = 1;
                    this.SelectedImageIndex = 1;
                    this.StateImageIndex = 1;
                }
                if (type == "file")
                {
                    switch (fp.Substring(fp.LastIndexOf(".") + 1))
                    {
                        case "jpg":
                        case "png":
                        case "bmp":
                        case "tif":
                        case "gif":
                            this.ImageIndex = 5;
                            this.SelectedImageIndex = 5;
                            this.StateImageIndex = 5;
                            break;

                        case "exe":
                        case "com":
                            this.ImageIndex = 4;
                            this.SelectedImageIndex = 4;
                            this.StateImageIndex = 4;
                            break;

                        case "bat":
                            this.ImageIndex = 10;
                            this.SelectedImageIndex = 10;
                            this.StateImageIndex = 10;
                            break;

                        case "mp3":
                        case "wav":
                        case "ogg":
                        case "mid":
                            this.ImageIndex = 3;
                            this.SelectedImageIndex = 3;
                            this.StateImageIndex = 3;
                            break;

                        default:
                            this.ImageIndex = 2;
                            this.SelectedImageIndex = 2;
                            this.StateImageIndex = 2;
                            break;
                    }
                }
            }
        }

        private void BuildTree(String path, TreeNodeCollection nodes, int subpath)
        {
            string[] _Directories = Directory.GetDirectories(path);

            foreach( string _SubDir in _Directories )
            {
                string _SubDirFolder = _SubDir.Substring( path.Length );
                nodes.Add(new myTreeNode (_SubDir,"dir"));
                _AccessList.Add(nodes[_SubDir].FullPath);
            }

            string[] Files = Directory.GetFiles(path);
            foreach (string _SubFiles in Files)
            {
                nodes.Add(new myTreeNode(_SubFiles,"file"));
                _AccessList.Add(nodes[_SubFiles].FullPath);
            }   


            for( int i = 0; i < _Directories.Length; i++ )
            {
                BuildTree( _Directories[ i ] + "\\", nodes[i].Nodes, subpath+1 );
                if (subpath < expand_deep)
                {
                    nodes[i].Expand();
                }
            }
        }

        private void Step_FolderRights_Load(object sender, System.EventArgs e)
        {
            this.treeView1.Nodes.Add(new myTreeNode(ProjectSettings.SourcePath,"dir"));
            BuildTree(ProjectSettings.SourcePath, this.treeView1.Nodes[0].Nodes, 0);
            this.treeView1.Nodes[0].Expand();
            _AccessList.Add(this.treeView1.Nodes[0].FullPath);
        }

        protected void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.label1.Text = e.Node.FullPath;
            this.label1.Visible = true;
            String tempstring = _AccessList.Find(List_Search);

            if (_AccessList.Exists(List_Search) && tempstring.Substring(0, tempstring.LastIndexOf("#@@#")) == this.label1.Text)
            {
                //MessageBox.Show(_AccessList.Find(List_Search).Substring(_AccessList.Find(List_Search).LastIndexOf("#@@#")));

                if (tempstring.Substring(tempstring.LastIndexOf("#@@#")).LastIndexOf("f") > 0)
                {
                    this.check_full.CheckState = CheckState.Checked;
                }
                else
                {
                    this.check_full.CheckState = CheckState.Unchecked;
                }

                if (tempstring.Substring(tempstring.LastIndexOf("#@@#")).LastIndexOf("w") > 0)
                {
                    this.check_write.CheckState = CheckState.Checked;
                }
                else
                {
                    this.check_write.CheckState = CheckState.Unchecked;
                }

                if (tempstring.Substring(tempstring.LastIndexOf("#@@#")).LastIndexOf("r") > 0)
                {
                    this.check_readonly.CheckState = CheckState.Checked;
                }
                else
                {
                    this.check_readonly.CheckState = CheckState.Unchecked;
                }

                if (tempstring.Substring(tempstring.LastIndexOf("#@@#")).LastIndexOf("m") > 0)
                {
                    this.check_modify.CheckState = CheckState.Checked;
                }
                else
                {
                    this.check_modify.CheckState = CheckState.Unchecked;
                }
            }
            else
            {
                this.check_readonly.CheckState = CheckState.Unchecked;
                this.check_write.CheckState = CheckState.Unchecked;
                this.check_modify.CheckState = CheckState.Unchecked;
                this.check_full.CheckState = CheckState.Unchecked;
            }
        }

        private void Step_FolderRights_Leave(object sender, EventArgs e)
        {
            ProjectSettings.AccessList = _AccessList;
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 0;
            e.Node.SelectedImageIndex = 0;
            e.Node.StateImageIndex = 0;
        }

        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 1;
            e.Node.SelectedImageIndex = 1;
            e.Node.StateImageIndex = 1;
        }

        private void check_write_CheckedChanged(object sender, EventArgs e)
        {
            String rights = "#@@#";
            _AccessList.Remove(this.label1.Text);
            _AccessList.RemoveAll(List_Search);

            if (this.check_modify.Checked == true && this.check_write.Checked == true)
            {
                this.check_full.Checked = true;
            }

            if (this.check_full.Checked == true && this.check_write.Checked == false)
            {
                this.check_full.CheckState = CheckState.Unchecked;
            }

            if (this.check_full.Checked == true)
            {
                rights = rights + "f";
            }

            if (this.check_write.Checked == true)
            {
                rights = rights + "w";
            }

            if (this.check_modify.Checked == true)
            {
                rights = rights + "m";
            }

            if (this.check_readonly.Checked == true)
            {
                rights = rights + "r";
            }

            if (rights != "#@@#")
            {
                _AccessList.Add(this.label1.Text + rights);
            }
            else
            {
                _AccessList.Add(this.label1.Text);
            }
        }

        private void check_modify_CheckedChanged(object sender, EventArgs e)
        {
            String rights = "#@@#";
            _AccessList.Remove(this.label1.Text);
            _AccessList.RemoveAll(List_Search);

            if (this.check_modify.Checked == true && this.check_write.Checked == true)
            {
                this.check_full.Checked = true;
            }

            if (this.check_full.Checked == true && this.check_modify.Checked == false)
            {
                this.check_full.CheckState = CheckState.Unchecked;
            }

            if (this.check_full.Checked == true)
            {
                rights = rights + "f";
            }

            if (this.check_write.Checked == true)
            {
                rights = rights + "w";
            }

            if (this.check_modify.Checked == true)
            {
                rights = rights + "m";
            }

            if (this.check_readonly.Checked == true)
            {
                rights = rights + "r";
            }

            if (rights != "#@@#")
            {
                _AccessList.Add(this.label1.Text + rights);
            }
            else
            {
                _AccessList.Add(this.label1.Text);
            }
        }

        private void check_full_CheckedChanged(object sender, EventArgs e)
        {
            if (this.check_full.Checked == true)
            {
                this.check_write.CheckState = CheckState.Checked;
                this.check_modify.CheckState = CheckState.Checked;
            }
            else
            {
                if (this.check_modify.Checked == true && this.check_write.Checked == true)
                {
                    this.check_write.CheckState = CheckState.Unchecked;
                    this.check_modify.CheckState = CheckState.Unchecked;
                }
            }
        }

        private void check_readonly_CheckedChanged(object sender, EventArgs e)
        {
            String rights = "#@@#";
            _AccessList.Remove(this.label1.Text);
            _AccessList.RemoveAll(List_Search);

            if (this.check_full.Checked == true)
            {
                rights = rights + "f";
            }

            if (this.check_write.Checked == true)
            {
                rights = rights + "w";
            }

            if (this.check_modify.Checked == true)
            {
                rights = rights + "m";
            }

            if (this.check_readonly.Checked == true)
            {
                rights = rights + "r";
            }

            if (rights != "#@@#")
            {
                _AccessList.Add(this.label1.Text + rights);
            }
            else
            {
                _AccessList.Add(this.label1.Text);
            }
        }

        private bool List_Search(String s)
        {
            if (s.LastIndexOf("#@@#") > 0 && s.Substring(0, s.LastIndexOf("#@@#")) == this.label1.Text)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
